package com.xy.mainp.huaban.widget;


import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.xy.FileUtil;
import com.xy.img.BitmapUtil;
import com.xy.mainp.R;
import com.xy.mainp.SaveImgBinding;
import com.xy.manager.MsgManager;

import java.io.File;
import java.io.IOException;

/**
 * Created by John on 2016/11/24.
 */

public class SaveImgPop extends PopupWindow implements View.OnClickListener {

    private Context mContext;
    private Drawable drawable;
    private final Bitmap bitmap;

    public SaveImgPop(Context context, Drawable drawable) {
        this.mContext = context;
        this.drawable = drawable;
        bitmap = ((GlideBitmapDrawable) drawable).getBitmap();
        init(context);
    }


    private void init(Context context) {
        SaveImgBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.view_save_img_popup, null, false);
        setContentView(binding.getRoot());
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);


        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        setAnimationStyle(R.style.AnimationPreview);

        binding.btnSaveImg.setOnClickListener(this);
        binding.btnBeWallpaper.setOnClickListener(this);
       // binding.btnBeLockWallpaper.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        File file = getFile();

        if (id == R.id.btn_save_img) {
            BitmapUtil.saveBitmap(bitmap, bitmap.hashCode() + ".jpg");
            new MsgManager(mContext).showToastMsg("保存图片成功");
        } else if (id == R.id.btn_be_wallpaper) {
            setWallPaper();
            BitmapUtil.saveBitmap(bitmap, bitmap.hashCode() + ".jpg");
        } /*else if (id == R.id.btn_be_lock_wallpaper) {
            BitmapUtil.saveBitmap(bitmap, file, 100, Bitmap.CompressFormat.JPEG);
            setLockWallPaper(file.getAbsolutePath());
        }*/
        brocastToDCIM(file);
        dismiss();
    }

    private File getFile() {
        File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "xyapp");
        if (dir == null) {
            dir = new File(Environment.getExternalStorageDirectory(), "xyapp");
        }
        dir = FileUtil.checkFile(dir);
        return new File(dir, bitmap.hashCode() + ".jpg");
    }

    private void setWallPaper() {
        try {
            WallpaperManager manager = WallpaperManager.getInstance(mContext);
            manager.setBitmap(bitmap);
            new MsgManager(mContext).showToastMsg("设置壁纸成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

   /* private void setLockWallPaper(String imgPath) {
        try {
            if (OSUtils.getOSName().equals(OSUtils.TYPE_FLYME)) {
                Class.forName("meizu.wallpaper.FlymeWallpaperManager");
                Class clz = Class.forName("com.android.server.wallpaper.FlymeWallpaperService");
                Method setLockWallpaper = clz.getMethod("setLockWallpaper", Bitmap.class);
                setLockWallpaper.invoke(clz, BitmapFactory.decodeFile(imgPath));
            } else {
                WallpaperManager mWallManager = WallpaperManager.getInstance(mContext);
                Class class1 = mWallManager.getClass();//获取类名
                Method setWallPaperMethod = class1.getMethod("setBitmapToLockWallpaper", Bitmap.class);//获取设置锁屏壁纸的函数
                setWallPaperMethod.invoke(mWallManager, BitmapFactory.decodeFile(imgPath));//调用锁屏壁纸的函数，并指定壁纸的路径imageFilesPath
            }
            new MsgManager(mContext).showToastMsg("锁屏壁纸设置成功");
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }*/

    private void brocastToDCIM(File file) {
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(file);
        intent.setData(uri);
        mContext.sendBroadcast(intent);
    }
}
